package zpin.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import util.dao.Pagination;
import zpin.dao.CityDao;
import zpin.dao.EngageDao;
import zpin.dao.ResumeDao;
import zpin.dao.TypeDao;
import zpin.model.City;
import zpin.model.Engage;
import zpin.model.Person;
import zpin.model.Resume;
import zpin.model.Type;

import com.opensymphony.xwork2.ActionSupport;


public class ResumeAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Resume resume;
	
	private ResumeDao resumeDao;
	
	private CityDao cityDao;
	
	private TypeDao typeDao;
	
	private List<City> cities;
	
	private List<Type> types;
	
	private Pagination<Resume> pagination;
	
	private List<Resume> findrbts;
	
	private Type type;
	
	private Long id;
	
	private Engage engage;
	
	 Set<Engage> engages;
	 private EngageDao engageDao;
		//获取数据库里的简历
		private List<Resume> voteResume; 
	
	
	
	
	public Set<Engage> getEngages() {
		return engages;
	}

	public void setEngages(Set<Engage> engages) {
		this.engages = engages;
	}

	public Engage getEngage() {
		return engage;
	}

	public void setEngage(Engage engage) {
		this.engage = engage;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public EngageDao getEngageDao() {
		return engageDao;
	}

	public void setEngageDao(EngageDao engageDao) {
		this.engageDao = engageDao;
	}

	
        
	public List<Resume> getVoteResume() {
		if(this.voteResume==null){
			this.voteResume=resumeDao.findAll();
		}
		return this.voteResume;
	}

	public void setVoteResume(List<Resume> voteResume) {
		this.voteResume = voteResume;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public List<Resume> getFindrbts() {
		return findrbts;
	}

	public void setFindrbts(List<Resume> findrbts) {
		this.findrbts = findrbts;
	}



	public Pagination<Resume> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Resume> pagination) {
		this.pagination = pagination;
	}

	public List<City> getCities() {
		if (this.cities == null) {
			this.cities = cityDao.findAll();
		}
		return this.cities;
	}

	public void setCities(List<City> cities) {
		this.cities = cities;
	}

	public List<Type> getTypes() {
		if(this.types==null){
			this.types=typeDao.findAll();
		}
		return this.types;
	}

	public void setTypes(List<Type> types) {
		this.types = types;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public TypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(TypeDao typeDao) {
		this.typeDao = typeDao;
	}

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public ResumeDao getResumeDao() {
		return resumeDao;
	}

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}
	//登陆简历的方法；
	@Transactional(propagation=Propagation.SUPPORTS)
	public String inputResume(){
		return SUCCESS;
	}
	
	public ResumeAction() {
		this.pagination = new Pagination<Resume>();
		this.pagination.setSize(5);
		this.pagination.setNo(1);
	}
	

	//保存简历
	public String saveResume(){
		//HttpServletRequest request = ServletActionContext.getRequest();
		
		
		
//		Person person = (Person) request.getSession().getAttribute("authorization");
//		
//		resume.setPerson(person);
		resumeDao.saveOrUpdate(resume);
		return SUCCESS;
		
	}
	//通过Resume的id得到对象
	public String updateResume(){
		this.resume=resumeDao.findById((this.resume.getId()));
		return SUCCESS;
	}
	//通过Resume的id得到对象
	public String get(){
		this.resume=resumeDao.findById((resume.getId()));
		return SUCCESS;
	}
	
	public String delete(){
		resumeDao.delete(resume);
		return SUCCESS;
	}
	
	public String browse(){
		
		resumeDao.browse(pagination);
		
		return SUCCESS;
	}

	public String browsevote(){
		ServletActionContext.getRequest().getSession().setAttribute("engageid",id);
		
		System.out.println(">>>>>>>>>>>>>>>>>="+id);
		//id=(Long) ServletActionContext.getRequest().getSession().getAttribute("engageid");
		 engage=engageDao.findById(id);
		 //engage=engageDao.findById(engage.getId());
		  //engages = new HashSet<Engage>();
	      //engages.add(engage);
//		  engages = new HashSet<Engage>();
//    	 engages.add(engage);
		resumeDao.browse(pagination);
		System.out.println(">>>>>>>>>>>>>>>>>="+id);
		return SUCCESS;
	}
     	
	public String findReByTy(){
		this.findrbts=resumeDao.findResumeByType(resume.getType().getId());

		return SUCCESS;
	}
	
     public String tovoteResume(){
    	 System.err.println("++++++++++++++++++++简历id="+resume.getId());
    	 System.err.println("++++++++++++++++++++招聘信息id="+id);
    	 id=(Long) ServletActionContext.getRequest().getSession().getAttribute("engageid");
    	 System.err.println("++++++++++++++++++++招聘信息id="+id);
		 engage=engageDao.findById(id);

		  engages = new HashSet<Engage>();
	      engages.add(engage);
	      
	      System.out.println("77777777777777777777777"+engages.size());
	   
	      
   	     resume=resumeDao.findById(resume.getId());
   	     resume.setEngages(engages);
   	     resumeDao.update(resume);
    	 return SUCCESS;
     }
   
}
